﻿namespace Core
{
    /// <summary>
    /// 存档系统
    ///
    /// 依赖FileSystem
    ///
    /// 绑定在具体的Game对象上
    /// </summary>
    public class BaseArchiveSystem : Core.Component, IAwake
    {
        public string Root { get; private set; }
        private PrefixDict prefixDict;

        public virtual void Awake()
        {
            Root = $"{G.Name}/ARCHIVE_DATA_112/";
            prefixDict = new PrefixDict(Root);
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();
        }

        public T Load<T>(string key)
        {
            var content = Load(key);
            var t = Utils.Json.Deserialize<T>(content);
            return t;
        }

        // public virtual void Save<T>(T t, string key)
        // {
        //     var content = Utils.Json.Serialize(t);
        //     Save(key, content);
        // }

        public string Load(string key)
        {
            key = prefixDict[key];
            return S.File.Read(key);
        }


        public void Save(string key, string content)
        {
            key = prefixDict[key];
            S.File.Write(key, content);
        }
    }
}